// pages/updateLogistics/updateLogistics.js
import api from '../../api/api'
const app = getApp()
var QQMapWX = require("../../utils/qqmap-wx-jssdk.min");
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deliveryPerson:"",
    deliveryPersonPhone:"",
    logisticsList: [],
    message: '',
    updatelog: {
      companyId: "",   //公司id
      shippingOrderNumber: "", //出通单号
      currentLocation: "", //当前位置信息
      latitude: "1", //纬度
      longitude: "1", //经度
    },
    steps: []
  },
  onClickLeft() {
    wx.navigateBack()
  },
  // //获取物流信息
  // logist() {
  //   let that = this
  //   let i=0
  //   api.post('/app/logistics/getLogisticsInfo', {shippingOrderNumber: that.data.updatelog.shippingOrderNumber})
  //     .then(res => {
  //       res.data.map(item=>{
  //         that.setData({
  //          [`steps[${i}].desc`]:item.createTime,
  //          [`steps[${i}].text`]:item.address
  //         })
  //         i=i+1
  //       })
  //       // that.setData({
  //       //   'steps.text':item.address
  //       // })
  //     })
  // },
  onSearch(e) {
    let that = this
    that.setData({
      'updatelog.currentLocation': e.detail
    })
  },

  getlocation() {
    let that = this
    wx.getLocation({
      type: 'gcj02', // 比较精确
      success: (res) => {
        that.setData({
          'updatelog.latitude' : res.latitude.toFixed(10),
          'updatelog.longitude' : res.longitude.toFixed(10),
        })
        that.jingWeiduToDiZhi();
      }
    })
  },
  jingWeiduToDiZhi() {
    let that = this;
    // 调用接口
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: that.data.updatelog.latitude,
        longitude: that.data.updatelog.longitude
      },
      success: function(res){
        let tempData = res.result.address_component;
        let  currentLocation = res.result.formatted_addresses.recommend
        that.setData({
          message :currentLocation,
          'updatelog.currentLocation': currentLocation
        });
        wx.showToast({
          icon:'none',
          title: "定位获取成功",
          duration: 1000
    })
      },
      fail: function(error) {
      },
      complete: function(res) {
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    that.setData({
      'updatelog.shippingOrderNumber': options.shippingOrderNumber
    })
    qqmapsdk = new QQMapWX({
      key: '2ASBZ-7SZWW-AN6RR-YZQDA-A67AQ-FYFMJ'
    });
    that.getlogist()
    that.getorderlist()
  },

  getlogist() {
    let that = this
    let id = wx.getStorageSync('id')
    api.post('/app/logistics/getLogisticsInfo', {id:id,shippingOrderNumber: that.data.updatelog.shippingOrderNumber})
      .then(res => {
        that.setData({
          logisticsList: res.data[0],
        })
      })
  },
  updatelogList(){
    let that = this
    api.post('/app/logistics/updateLogisticsInfo', that.data.updatelog)
    .then(res => {
      if (res.code==200) {
        wx.showToast({
          icon:'none',
          title: "更新地址成功",
          duration: 2000
    })
        wx.navigateBack()
      } else {
        wx.showToast({
          icon:'none',
          title: "更新地址失败",
          duration: 2000
    })
      }
    })
  },
  //获取订单详情
  getorderlist() {
    // 改变this指向
    let that = this;
    api.post('/app/logistics/getOrderDetail', {shippingOrderNumber: that.data.updatelog.shippingOrderNumber})
    .then(res => {
      that.setData({
        deliveryPerson:res.data.deliveryPerson,
        deliveryPersonPhone:res.data.deliveryPersonPhone,
      })
    })
    .catch(err => {
    })
},
  upateAddress(){
    let that=this
    let companyid = wx.getStorageSync('companyid')
    that.setData({
      'updatelog.companyId':companyid 
    })
    that.updatelogList()
  },
})